import React from "react";
import {
  Form,
  Input,
  Button,
  Dialog,
} from "antd-mobile";
import { useNavigate } from "react-router-dom";
import { userLogin } from "../../service/userService";

// eslint-disable-next-line import/no-anonymous-default-export
export default () => {
  const navigate = useNavigate();
  const onFinish = async (values) => {
    let { data } = await userLogin(values);
    if (data.code == 200) {
      window.localStorage.setItem("token", data.token);
      navigate("/")
    } else {
      Dialog.alert({
        content: <pre>{data.msg}</pre>,
      });
    }
  };

  return (
    <>
      <Form
        layout="horizontal"
        onFinish={onFinish}
        footer={
          <Button block type="submit" color="primary" size="large">
            提交
          </Button>
        }
      >
        <Form.Item
          name="username"
          label="姓名"
          rules={[{ required: true, message: "姓名不能为空" }]}
        >
          <Input placeholder="请输入姓名" />
        </Form.Item>

        <Form.Item
          name="password"
          label="密码"
          rules={[{ required: true, message: "密码不能为空" }]}
        >
          <Input placeholder="请输入密码" type="password" />
        </Form.Item>
      </Form>
    </>
  );
};
